package com.homedo.odin.solution.service.bizservice.mng;

import com.github.pagehelper.PageInfo;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.mng.FinanceListPriResponseBean;
import com.homedo.odin.solution.contract.pc.OmsFinanceListResponseBean;
import com.homedo.odin.solution.enumcode.basic.BusinessTypeEnum;
import com.homedo.odin.solution.enumcode.solution.SolutionTypeEnum;
import com.homedo.odin.solution.model.dto.PageBean;
import com.homedo.odin.solution.model.dto.PcAccountInfo;
import com.homedo.odin.solution.model.entity.Solution;
import com.homedo.odin.solution.model.entity.Specification;
import com.homedo.odin.solution.model.entity.UserPayOrder;
import com.homedo.odin.solution.service.SolutionServiceImpl;
import com.homedo.odin.solution.service.SpecificationServiceImpl;
import com.homedo.odin.solution.service.UserPayOrderServiceImpl;
import com.homedo.odin.solution.service.common.impl.HomedoServiceImpl;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: ZhangLei
 * @date: 2018/4/9
 * @Description:
 */
@Service
public class MngFinanceManageService {

    @Autowired
    private UserPayOrderServiceImpl userPayOrderService;
    @Autowired
    private HomedoServiceImpl homedoService;
    @Autowired
    private SolutionServiceImpl solutionService;
    @Autowired
    private SpecificationServiceImpl specificationService;

    public OmsFinanceListResponseBean queryFinancialListForPri(Integer businessType, Long startCreatTime,
            Long endCreateTime, Integer currentPage, Integer pageSize) {
        Date createStartDate = null;
        Date createEndDate = null;
        if (startCreatTime != null && endCreateTime != null) {
            if (startCreatTime > endCreateTime) {
                throw new BizException(Meta.CODE_451.getCode(), "起始时间参数非法");
            }
            createStartDate = new Date(startCreatTime);
            createEndDate = new Date(endCreateTime);
        }

        PageBean pageBean = new PageBean(currentPage, pageSize);

        PageInfo<UserPayOrder> queryResult = userPayOrderService
                .queryFinancialListForPri(businessType, createStartDate, createEndDate, pageBean);
        BigDecimal sum = userPayOrderService.queryFinancialCount();

        List<UserPayOrder> userPayOrderList = queryResult.getList();
        List<FinanceListPriResponseBean> resultList = new ArrayList<>();

        if (CollectionUtils.isEmpty(userPayOrderList)) {
            return null;
        }

        userPayOrderList.parallelStream().forEach(userPayOrder -> {
            FinanceListPriResponseBean response = getFinanceListPriResponseBean(userPayOrder, sum);
            if (userPayOrder.getBusinessType().intValue() == BusinessTypeEnum.SOLUTION.getDbValue()) {
                solutionExcutor(userPayOrder, response);
            } else if (userPayOrder.getBusinessType().intValue() == BusinessTypeEnum.SPECIFICATION.getDbValue()) {
                specificationExcutor(userPayOrder, response);
            }
            resultList.add(response);
        });

        OmsFinanceListResponseBean transfrom = new OmsFinanceListResponseBean();
        transfrom.setPageNum(queryResult.getPageNum());
        transfrom.setList(resultList);
        transfrom.setPages(queryResult.getPages());
        transfrom.setPageSize(queryResult.getPageSize());
        transfrom.setTotal(queryResult.getTotal());

        return transfrom;
    }

    private void specificationExcutor(UserPayOrder userPayOrder, FinanceListPriResponseBean response) {
        Specification specification = specificationService.getSpecification(userPayOrder.getBusinessId());
        if (specification != null) {
            response.setBelonger(specification.getUserId());
            response.setBelongerName(specification.getUserName());
            if (NumberHelper.validatePositive(specification.getUserId())) {
                response.setProfit(userPayOrder.getAmount().multiply(new BigDecimal(0.5)));
            } else {
                response.setProfit(userPayOrder.getAmount());
            }
            response.setType(0);
        }
    }

    private void solutionExcutor(UserPayOrder userPayOrder, FinanceListPriResponseBean response) {
        Solution solution = solutionService.getSolution(userPayOrder.getBusinessId());
        if (solution != null) {
            response.setBelonger(solution.getUserId());
            response.setBelongerName(solution.getUserName());

            if (NumberHelper.validatePositive(solution.getUserId())) {
                response.setProfit(userPayOrder.getAmount().multiply(new BigDecimal(0.5)));
            } else {
                response.setProfit(userPayOrder.getAmount());
            }

            if (solution.getSolutionType().intValue() == SolutionTypeEnum.BRAND_SOLUTION.getDbValue()) {
                response.setType(2);
            } else {
                response.setType(1);
            }
        }
    }

    private FinanceListPriResponseBean getFinanceListPriResponseBean(UserPayOrder userPayOrder, BigDecimal sum) {
        FinanceListPriResponseBean response = new FinanceListPriResponseBean();
        response.setOrderNo(userPayOrder.getOrderNo());
        response.setCreateTime(userPayOrder.getCreateTime());
        response.setAmount(userPayOrder.getAmount());
        response.setPayMode(userPayOrder.getPayMode());
        response.setUserId(userPayOrder.getUid());

        //获取用户名称
        PcAccountInfo userMessageInfo = homedoService.getUserMessageInfo(userPayOrder.getUid());
        if (null != userMessageInfo) {
            response.setUserName(userMessageInfo.getName());
        }

        if (NumberHelper.validatePositive(userPayOrder.getBusinessId())) {
            response.setBusinessId(userPayOrder.getBusinessId());
        }
        response.setBusinessName(userPayOrder.getBusinessName());
        response.setBusinessType(userPayOrder.getBusinessType());
        response.setTotalAmout(sum);
        return response;
    }


}
